package com.znufe.exam.service.excel;

import com.alibaba.excel.util.StringUtils;
import com.znufe.exam.utility.easyexcel.ExcelCheckErrDto;
import com.znufe.exam.utility.easyexcel.ExcelCheckResult;
import com.znufe.exam.viewmodel.admin.question.excelmodel.SingleChoiceFromExcelVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SingleChoiceServiceImpl implements SingleChoiceService {

    @Autowired
    public SingleChoiceServiceImpl() {}

    @Override
    public ExcelCheckResult checkImportExcel(List<SingleChoiceFromExcelVM> singleChoiceList, Integer questionType) {
        //成功结果集
        List<SingleChoiceFromExcelVM> successList = new ArrayList<>();
        //错误数组
        List<ExcelCheckErrDto<SingleChoiceFromExcelVM>> errList = new ArrayList<>();

        for (SingleChoiceFromExcelVM singleChoice : singleChoiceList) {
            // 错误信息
            StringBuilder errMsg = new StringBuilder();
            if (null == singleChoice.getTitle()) {
                errMsg.append("单选题题干不能为空！请修改").append(";");
            }
            if (null == singleChoice.getOptionA() || null == singleChoice.getOptionB()) {
                errMsg.append("选项A或选项B的内容为空，请至少设置两个选项内容！").append(";");
            }
            if (null == singleChoice.getCorrect()) {
                errMsg.append("答案不能为空！请为题目设置正确答案！").append(";");
            } else {
                String correct = singleChoice.getCorrect();
                switch (correct) {
                    case "A": {
                        if (null == singleChoice.getOptionA()) {
                            errMsg.append("选项A的内容为空，请重新设置正确答案！").append(";");
                        }
                        break;
                    }
                    case "B": {
                        if (null == singleChoice.getOptionB()) {
                            errMsg.append("选项B的内容为空，请重新设置正确答案！").append(";");
                        }
                        break;
                    }
                    case "C": {
                        if (null == singleChoice.getOptionC()) {
                            errMsg.append("选项C的内容为空，请重新设置正确答案！").append(";");
                        }
                        break;
                    }
                    case "D": {
                        if (null == singleChoice.getOptionD()) {
                            errMsg.append("选项D的内容为空，请重新设置正确答案！").append(";");
                        }
                        break;
                    }
                    case "E": {
                        if (null == singleChoice.getOptionE()) {
                            errMsg.append("选项E的内容为空，请重新设置正确答案！").append(";");
                        }
                        break;
                    }
                    case "F": {
                        if (null == singleChoice.getOptionF()) {
                            errMsg.append("选项F的内容为空，请重新设置正确答案！").append(";");
                        }
                        break;
                    }
                    default:
                        break;
                }
            }
            if (null == singleChoice.getScore()) {
                errMsg.append("分数不能为空！请为题目设置分数！").append(";");
            }
            // 依据上面合法性判断的结果，
            // 正确的对象信息添加进successList，错误的对象信息添加进errList
            if (StringUtils.isEmpty(errMsg.toString())) {
                // 这里有两个选择，1、一个返回成功的对象信息，2、进行持久化操作
                System.out.println("SingleChoiceServiceImpl的 checkImportExcel里的 ：" + singleChoice);
                successList.add(singleChoice);
            } else {
                // 添加错误信息
                errList.add(new ExcelCheckErrDto(singleChoice, errMsg.toString()));
            }
        }
        return new ExcelCheckResult(successList,errList);
    }
}
